﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Tailor_IMG_Entity : EntityBase<Tailor_IMG>
    {
        public override RESP_Info Add(Tailor_IMG model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into TAILOR_GALLERY(TAILOR_ID,PHOTO_ID) values (@TAILOR_ID,@PHOTO_ID)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("Tailor_ID", model.Tailor_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", model.PHOTO_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加定制师照片成功</p></h5>";
                    info.Url = "TailorList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加定制师照片失败</p></h5>";
                    info.Url = "TailorAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加定制师照片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(Tailor_IMG model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update TAILOR_GALLERY set PHOTO_ID=@PHOTO_ID where TAILOR_ID=@TAILOR_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TAILOR_ID", model.Tailor_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", model.PHOTO_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑定制师照片成功</p></h5>";
                    info.Url = "TailorList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑定制师照片失败</p></h5>";
                    info.Url = "TailorAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑定制师照片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(Tailor_IMG model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from TAILOR_GALLERY where TAILOR_ID=@TAILOR_ID AND PHOTO_ID=@PHOTO_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TAILOR_ID", model.Tailor_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", model.PHOTO_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除定制师照片成功</p></h5>";
                    info.Url = "TailorList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除定制师照片失败</p></h5>";
                    info.Url = "TailorList.aspx";

                    Tom.Net.Log.LogManager.Error("删除定制师照片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override Tailor_IMG GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select Tailor_ID,PHOTO_ID from TAILOR_GALLERY where Tailor_ID=@Tailor_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("Tailor_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Tailor_IMG model = new Tailor_IMG();
                        if (!reader.IsDBNull(0)) model.Tailor_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) model.PHOTO_ID = reader.GetString(1);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return model;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制师照片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override Tailor_IMG GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Tailor_IMG routeGallery = new Tailor_IMG();
                        if (!reader.IsDBNull(0)) routeGallery.Tailor_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeGallery.PHOTO_ID = reader.GetString(1);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeGallery;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制师照片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<Tailor_IMG> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<Tailor_IMG> routeGallerys = new List<Tailor_IMG>();
                    while (reader.Read())
                    {
                        Tailor_IMG routeGallery = new Tailor_IMG();
                        if (!reader.IsDBNull(0)) routeGallery.Tailor_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeGallery.PHOTO_ID = reader.GetString(1);

                        routeGallerys.Add(routeGallery);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return routeGallerys;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取定制师照片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
